package com.yuyue.springFramework.note05;

import com.yuyue.springFramework.note05.annotation.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * 《Spring-5 注解》文章代码
 *  文章地址：https://mp.weixin.qq.com/s/TK2TYlooKXiD3atTtCwgzQ
 */
public class Note05Test {
    @Test
    //@Autowired 自动填充
    public void fun1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        RequireEntity requireEntity = applicationContext.getBean("requireEntity", RequireEntity.class);
        //打印自动填充对象
        System.out.println(requireEntity.getArgsOne());
    }

    @Test
    //@Autowired 多参数自动填充
    public void fun1_1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        RequireEntity2 requireEntity2 = applicationContext.getBean("requireEntity2", RequireEntity2.class);
        //打印自动填充对象
        System.out.println(requireEntity2);
    }

    @Test
    //@Resource 自动填充
    public void fun2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        ResourceEntity resourceEntity = applicationContext.getBean("resourceEntity", ResourceEntity.class);
        //打印自动填充对象
        System.out.println(resourceEntity.getArgsOne());
        System.out.println(resourceEntity.getArgsOne2());
    }

    @Test
    //@Value 自动填充
    public void fun3(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        ValueEntity valueEntity = applicationContext.getBean("valueEntity", ValueEntity.class);
        //打印自动填充对象
        System.out.println(valueEntity.i);
        System.out.println(valueEntity.n);

        System.out.println(valueEntity.environment.getProperty("test.name"));
    }

    @Test
    //@Component 注册Bean
    public void fun4(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        ComponentEntity componentEntity = applicationContext.getBean("componentEntity", ComponentEntity.class);

        //打印自动填充对象
        System.out.println(componentEntity);
    }

    @Test
    //@Bean 注册Bean
    public void fun5(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        ComponentEntity componentEntity = applicationContext.getBean("fComponent", ComponentEntity.class);

        //打印自动填充对象
        System.out.println(componentEntity);
    }

    @Test
    //@Bean 注册Bean，在@Component注解的类中相互调用
    public void fun5_1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        ComponentEntity componentEntity = applicationContext.getBean("fComponent", ComponentEntity.class);
        ComponentEntity1 componentEntity1 = applicationContext.getBean("fComponent1", ComponentEntity1.class);

        //打印自动填充对象
        System.out.println(componentEntity);
        System.out.println("-----------");
        System.out.println(componentEntity1.getComponentEntity());
    }

    @Test
    //@Bean 注册Bean，在@Configuration注解的类中相互调用
    public void fun5_2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext03.xml");
        ComponentEntity componentEntity3 = applicationContext.getBean("fComponent3", ComponentEntity.class);
        ComponentEntity1 componentEntity4 = applicationContext.getBean("fComponent4", ComponentEntity1.class);

        //打印自动填充对象
        System.out.println(componentEntity3);
        System.out.println("-----------");
        System.out.println(componentEntity4.getComponentEntity());
    }

    @Test
    //不依赖xml实例化容器
    public void fun6(){
        //构造方法加载
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentEntity.class);
        ComponentEntity componentEntity = applicationContext.getBean(ComponentEntity.class);

        System.out.println(componentEntity);
    }

    @Test
    //纯java方式扫描bean，方式一
    public void fun7(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //扫描bean
        applicationContext.scan("com");
        //刷新方法只能调用一次，因此只能使用无参的AnnotationConfigApplicationContext构造
        applicationContext.refresh();

        ComponentEntity componentEntity = applicationContext.getBean("componentEntity", ComponentEntity.class);
        System.out.println(componentEntity);
    }

    @Test
    //纯java方式扫描bean，方式二
    public void fun8(){
        //加载配置类，通过配置类上的 @ComponentScan(basePackages = "com") 进行扫描
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

        ComponentEntity componentEntity = applicationContext.getBean("componentEntity", ComponentEntity.class);
        System.out.println(componentEntity);
    }

    @Test
    //@ImportResource 引入xml配置文件
    public void fun9(){
        //无参构造实例化容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //指定活动状态的配置文件，未指定的情况下使用默认配置 default
        applicationContext.getEnvironment().setActiveProfiles("development");
        //注册bean
        applicationContext.register(AppConfig.class);
        //刷新容器
        applicationContext.refresh();

    }

    @Test
    //@ImportResource 引入xml配置文件
    public void fun10(){
        //加载配置类，通过配置类上的 @ImportResource("classpath:config.xml") 引入配置文件
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

        DriverManagerDataSource driverManagerDataSource = applicationContext.getBean(DriverManagerDataSource.class);
        System.out.println(driverManagerDataSource.getUsername());
        System.out.println(driverManagerDataSource.getPassword());
    }
}
